package com.itheima.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.Announcement;
import com.itheima.mapper.AnnouncementMapper;
import com.itheima.pojo.po.AnnouncementPo;
import com.itheima.query.AnnouncementPageQuery;
import com.itheima.query.ProjectPageQuery;
import com.itheima.result.PageResult;
import com.itheima.service.AnnouncementService;
import com.itheima.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author guanhuaer
 * @since 2024-09-05
 */
@RestController
@RequestMapping("/cement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService cementService;
    @Autowired
    private AnnouncementMapper cementMapper;

    @PostMapping("/cementPage")
    @ApiOperation("公告分页查询")
    public Result<PageResult> cementPage(@RequestBody AnnouncementPageQuery cementPageQuery) {
        if (cementPageQuery.getPageSize() == null || cementPageQuery.getPageNum() == null) {
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        PageResult pageResult = cementService.cementPage(cementPageQuery);
        return Result.success(pageResult);
    }

    @GetMapping("/cementTitle")
    @ApiOperation("公告标题查询")
    public Result cementTitle(String title) {
        System.out.println(title);
        if (title == null || title.equals("")) {
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        Announcement announcement = cementMapper.selectTitle(title);
        if (announcement == null) {
            return Result.success();
        }
        return Result.error("公告名称已存在");
    }

    @PostMapping("/cementAdd")
    @ApiOperation("公告添加")
    public Result cementAdd(@RequestBody List<AnnouncementPo> cementPos) {
        for (AnnouncementPo cementPo : cementPos) {
            if (cementPo.getTitle() == null || cementPo.getContent() == null ||
                    cementPo.getType() == null) {
                return Result.error(MessageConstant.ACCOUNT_CHANSHU);
            }
            cementService.cementAdd(cementPo);
        }
        return Result.success();
    }
    @PostMapping("/cementEdit")
    @ApiOperation("公告标题查询")
    public Result cementEdit(@RequestBody AnnouncementPo cementPo) {
        if (cementPo.getTitle() == null || cementPo.getContent() == null ||
                cementPo.getId() == null || cementPo.getType() == null) {
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        cementService.cementAEdit(cementPo);
        return Result.success();
    }

    @PostMapping("/cementDelects")
    @ApiOperation("分类删除")
    public Result cementDelects(@RequestBody String Ids){
        String string = Ids.substring(8,Ids.length()-2);
        if (string.length()<1){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        cementService.Delects(string);
        return Result.success();
    }

    @GetMapping("/announCement")
    @ApiOperation("分类删除")
    public Result announCement(){
        List<Announcement> announcementList = cementMapper.selectAll();
        return Result.success(announcementList);
    }



}
